package com.example.administrator.baikangxing.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.administrator.baikangxing.MyApplication;
import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.fragment.CallFragment;
import com.example.administrator.baikangxing.fragment.HomeFragment;
import com.example.administrator.baikangxing.fragment.LocationFragment;
import com.example.administrator.baikangxing.fragment.MeFragment;
import com.example.administrator.baikangxing.fragment.ServiceFragment;
import com.example.administrator.baikangxing.ronglian.SDKCoreHelper;

import java.lang.reflect.Field;

import cn.sharesdk.framework.ShareSDK;

import static com.example.administrator.baikangxing.MyApplication.activitys;

public class MainActivity extends FragmentActivity {

    private SparseArray<Fragment> list;
    private RadioGroup main_rg;
    private int currentIndex = 0;
    long exitSytemTime = 0;    // 返回键退出程序

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //去掉标题栏
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//防止软件盘弹出,使界面变形
        setContentView(R.layout.activity_main);
        activitys.add(MainActivity.this);
        setTab();
        initView();
        initData();
        initListener();
        init();
    }

    private void init() {
        SDKCoreHelper helper = new SDKCoreHelper(MyApplication.context);
        ShareSDK.initSDK(MyApplication.context);//初始化sharesdk
    }

    private void setTab() {
        //当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            LinearLayout linear_bar = (LinearLayout) findViewById(R.id.base_ll_bar);
            linear_bar.setVisibility(View.VISIBLE);
            //获取到状态栏的高度
            int statusHeight = getStatusBarHeight();
            //动态的设置隐藏布局的高度
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linear_bar.getLayoutParams();
            params.height = statusHeight;
            linear_bar.setLayoutParams(params);
            linear_bar.setBackgroundResource(R.color.orangered);
        }
    }

    /**
     * 通过反射的方式获取状态栏高度
     *
     * @return
     */
    private int getStatusBarHeight() {
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(obj).toString());
            return getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public void initView() {
        main_rg = (RadioGroup) findViewById(R.id.main_rg);
    }

    public void initListener() {
        main_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.main_rb_home://首页
                        switchFragment(0);
                        break;
                    case R.id.main_rb_location://定位
                        switchFragment(1);
                        break;
                    case R.id.main_rb_call://通话
                        switchFragment(2);
                        break;
                    case R.id.main_rb_service://客服
                        switchFragment(3);
                        break;
                    case R.id.main_rb_me://我的
                        switchFragment(4);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    public void initData() {
        list = new SparseArray<>();
        list.put(0, new HomeFragment());
        list.put(1, new LocationFragment());
        list.put(2, new CallFragment());
        list.put(3, new ServiceFragment());
        list.put(4, new MeFragment());
        getSupportFragmentManager().beginTransaction().add(R.id.main_frame, list.get(0)).commit();
    }

    //点击替换相应的fragment
    private void switchFragment(int index) {
        Fragment targetFragment = list.get(index);
        //当前Fragment
        Fragment currentFragment = list.get(currentIndex);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (targetFragment.isAdded()) {
            //目标Fragment已经被添加了。。。则隐藏正在展示的Fragment，显示目标的fragment
            transaction.hide(currentFragment).show(targetFragment);
        } else {
            //没有被添加...则隐藏当前的Fragment,添加目标Fragment.
            transaction.hide(currentFragment).add(R.id.main_frame, targetFragment);
        }
        //提交事务
        transaction.commitAllowingStateLoss();
        currentIndex = index;

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - exitSytemTime > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitSytemTime = System.currentTimeMillis();
                return true;
            } else {
                finish();
            }

        }
        return super.onKeyDown(keyCode, event);
    }
}
